HAL PWR typical use cases for migration ¶
HAL PWR Enter Stop Mode with RTC Wakeup ¶
|
HAL1 |
HAL2 |
|
|---|---|---|
|
Usage |
// RTC handle
RTC_HandleTypeDef hrtc;
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
int main(void)
{
// HAL initialization
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Initialize RTC
hrtc.Instance = RTC;
HAL_RTC_Init(&hrtc);
// Enable PWR clock
__HAL_RCC_PWR_CLK_ENABLE();
// Configure Voltage Scaling (Scale 1 for low-power modes)
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
// Configure the RTC as a wakeup source
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xFFFF, RTC_WAKEUPCLOCK_RTCCLK_DIV16 , 0);
// Enter Stop Mode (Stop 0 with low-power regulator)
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
// System resumes here after wakeup
SystemClock_Config(); // Reconfigure the system clock
while (1)
{
// Main loop
}
}
|
hal_rtc_wakeup_config_t config_wakeup;
hal_rtc_time_t period_reload;
hal_rtc_time_t period_autoclear;
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
int main(void)
{
// HAL initialization
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Initialize RTC
HAL_RTC_DisableWriteProtection()
HAL_RTC_EnterInitMode()
// Enable PWR clock
HAL_RCC_PWR_EnableClock();
// Configure Voltage Scaling (Scale 1 for low-power modes)
HAL_PWR_SetMainRegulatorVoltageScalingRange(HAL_PWR_VOLT_SCALE_1);
// Configure the RTC as a wakeup source
config_wakeup.clock = HAL_RTC_WAKEUP_TIMER_CLOCK_RTCCLK_DIV16;
period_reload.sec = 1U;
HAL_RTC_WAKEUP_SetConfig(&config_wakeup);
HAL_RTC_WAKEUP_SetPeriod(&period_reload, &period_autoclear);
HAL_RTC_WAKEUP_Start(HAL_RTC_WAKEUP_IT_ENABLE);
// Enter Stop Mode (Stop 0 with low-power regulator)
HAL_PWR_EnterSTOPMode(HAL_PWR_LOW_PWR_MODE_WFI, HAL_PWR_STOP0_MODE);
// System resumes here after wakeup
SystemClock_Config(); // Reconfigure the system clock
while (1)
{
// Main loop
}
}
|
HAL PWR Enter Stop Mode with External Wakeup ¶
|
HAL1 |
HAL2 |
|
|---|---|---|
|
Usage |
// Define the GPIO pin for external wakeup (e.g., PA0)
#define WAKEUP_PIN GPIO_PIN_0
#define WAKEUP_PORT GPIOA
void SystemClock_Config(void);
int main(void)
{
// Initialize the HAL Library
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Enable the PWR clock
__HAL_RCC_PWR_CLK_ENABLE();
// Configure Voltage Scaling (Scale 1 for low-power modes)
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
// Initialize the GPIO pin for external wakeup
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable the GPIO clock for the wakeup pin
__HAL_RCC_GPIOA_CLK_ENABLE();
// Configure the GPIO pin as an external interrupt
GPIO_InitStruct.Pin = WAKEUP_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // Trigger on falling edge
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down
HAL_GPIO_Init(WAKEUP_PORT, &GPIO_InitStruct);
// Enable and set the EXTI interrupt priority
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
// Enter Stop Mode
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
// System resumes here after wakeup
SystemClock_Config(); // Reconfigure the system clock
while (1)
{
// Main loop
}
}
// EXTI interrupt handler for the wakeup pin
void EXTI0_IRQHandler(void)
{
// Handle the interrupt
HAL_GPIO_EXTI_IRQHandler(WAKEUP_PIN);
}
// Callback function for EXTI interrupt
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
// Wakeup event occurred
// Add any additional logic here if needed
}
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
|
// Define the GPIO pin for external wakeup (e.g., PA0)
#define WAKEUP_PIN HAL_GPIO_PIN_0
#define WAKEUP_PORT GPIOA
void SystemClock_Config(void);
int main(void)
{
// Initialize the HAL Library
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Enable the PWR clock
HAL_RCC_PWR_EnableClock();
// Configure Voltage Scaling (Scale 1 for low-power modes)
HAL_PWR_SetMainRegulatorVoltageScalingRange(HAL_PWR_VOLT_SCALE_1);
// Initialize the GPIO pin for external wakeup
hal_gpio_config_t GPIO_ConfigStructure;
hal_exti_handle_t hexti;
hal_exti_config_t EXTI_ConfigStructure;
GPIO_ConfigStructure.mode = HAL_GPIO_MODE_INPUT;
GPIO_ConfigStructure.pull = HAL_GPIO_PULL_NO;
HAL_GPIO_Init(WAKEUP_PORT, WAKEUP_PIN,
&GPIO_ConfigStructure);
HAL_EXTI_Init(&hexti, HAL_EXTI_LINE_0);
EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_FALLING;
EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT)
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
// Enter Stop Mode
HAL_PWR_EnterSTOPMode(HAL_PWR_LOW_PWR_MODE_WFI, HAL_PWR_STOP0_MODE);
// System resumes here after wakeup
SystemClock_Config(); // Reconfigure the system clock
while (1)
{
// Main loop
}
}
// EXTI interrupt handler for the wakeup pin
void EXTI0_IRQHandler(void)
{
// Handle the interrupt
HAL_EXTI_IRQHandler(&hexti);
}
// Callback function for EXTI interrupt
void HAL_EXTI_TriggerCallback(uint16_t GPIO_Pin)
{
// Wakeup event occurred
// Add any additional logic here if needed
}
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
|
Power Voltage Detector (PVD) with EXTI wakeup ¶
|
HAL1 |
HAL2 |
|
|---|---|---|
|
Usage |
#include "stm32u5xx_hal.h"
// Function Prototypes
void SystemClock_Config(void);
// Main Function
int main(void)
{
// Initialize the HAL Library
HAL_Init();
// Configure the System Clock
SystemClock_Config();
// Configure the Power Voltage Detector (PVD)
PWR_PVDTypeDef sConfigPVD;
// Enable the PWR Clock
HAL_RCC_PWR_EnableClock();
// Configure the PVD Level and Mode
sConfigPVD.PVDLevel = PWR_PVDLEVEL_3; // Set threshold (e.g., 2.5V)
sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING_FALLING; // Interrupt on both rising and falling edges
// Initialize the PVD
HAL_PWR_ConfigPVD(&sConfigPVD);
// Enable the PVD Interrupt
HAL_PWR_EnablePVD();
// Configure the EXTI Line for PVD
HAL_NVIC_SetPriority(PVD_AVD_IRQn, 0, 0); // Set priority
HAL_NVIC_EnableIRQ(PVD_AVD_IRQn); // Enable the interrupt
// Enter Sleep Mode and wait for PVD interrupt
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
// Main loop (should not reach here unless woken up)
while (1)
{
// Add your application code here
}
}
// PVD Interrupt Handler
void HAL_PWR_PVDCallback(void)
{
// This callback is triggered when the PVD detects a voltage threshold crossing
if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO))
{
// Voltage is below the threshold
}
else
{
// Voltage is above the threshold
}
}
// System Clock Configuration (Example for STM32U5)
void SystemClock_Config(void)
{
}
// PVD IRQ Handler
void PVD_AVD_IRQHandler(void)
{
HAL_PWR_PVD_IRQHandler();
}
|
#include "stm32u5xx_hal.h"
// Function Prototypes
void SystemClock_Config(void);
// Main Function
int main(void)
{
// Initialize the HAL Library
HAL_Init();
// Configure the System Clock
SystemClock_Config();
// Enable the PWR Clock
HAL_RCC_PWR_EnableClock();
// Configure the PVD Level and Mode
HAL_PWR_SetProgrammableVoltageDetectorLevel(HAL_PWR_PVD_LEVEL_3);
// Initialize the PVD
EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_RISING_FALLING;
EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT);
// Enable the PVD Interrupt
HAL_PWR_EnableProgrammableVoltageDetector();
// Configure the EXTI Line for PVD
HAL_NVIC_SetPriority(PVD_AVD_IRQn, 0, 0); // Set priority
HAL_NVIC_EnableIRQ(PVD_AVD_IRQn); // Enable the interrupt
// Enter Sleep Mode and wait for PVD interrupt
HAL_PWR_EnterSLEEPMode(HAL_PWR_LOW_PWR_MODE_WFI);
// Main loop (should not reach here unless woken up)
while (1)
{
// Add your application code here
}
}
// PVD Interrupt Handler
void HAL_EXTI_TriggerCallback(void)
{
// This callback is triggered when the PVD detects a voltage threshold crossing
if (LL_PWR_IsActiveFlag_PVDO(LL_PWR_FLAG_PVDO))
{
// Voltage is below the threshold
}
else
{
// Voltage is above the threshold
}
}
// System Clock Configuration (Example for STM32U5)
void SystemClock_Config(void)
{
}
void EXTI0_IRQHandler(void)
{
HAL_EXTI_IRQHandler(&hexti);
}
|
HAL PWR Enter Standby Mode with External Wakeup ¶
|
HAL1 |
HAL2 |
|
|---|---|---|
|
Usage |
// Function prototypes
void SystemClock_Config(void);
int main(void)
{
// HAL initialization
HAL_Init();
SystemClock_Config();
// Initialize GPIO for EXTI
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIOA clock
__HAL_RCC_GPIOA_CLK_ENABLE();
// Configure PA0 as input with EXTI interrupt
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; // Trigger on rising edge
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Enable and set EXTI line 0 interrupt priority
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
// Enter standby mode
// Enable Power Control clock
__HAL_RCC_PWR_CLK_ENABLE();
// Clear the Wakeup flag
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
// Enable EXTI wakeup source
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1_HIGH);
// Enter Standby mode
HAL_PWR_EnterSTANDBYMode();
// Code should never reach here after entering standby mode
while (1)
{
}
}
// System Clock Configuration (example for STM32U5)
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
|
// Function prototypes
void SystemClock_Config(void);
int main(void)
{
// HAL initialization
HAL_Init();
SystemClock_Config();
// Enable GPIOA clock
HAL_RCC_GPIOA_CLK_ENABLE();
EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_RISING_FALLING;
EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT);
// Enable and set EXTI line 0 interrupt priority
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
// Enter standby mode
// Enable Power Control clock
HAL_RCC_PWR_EnableClock();
// Clear the Wakeup flag
LL_PWR_ClearFlag_WU1();
// Enable EXTI wakeup source
HAL_PWR_LP_SetConfigWakeupPin(HAL_PWR_WAKEUP_PIN_1, HAL_PWR_WAKEUP_PIN_POLARITY_HIGH)
// Enter Standby mode
HAL_PWR_EnterSTANDBYMode();
// Code should never reach here after entering standby mode
while (1)
{
}
}
// System Clock Configuration (example for STM32U5)
void SystemClock_Config(void)
{
// Reinitialize the system clock after waking up
// (Implementation depends on your specific clock configuration)
// Example: Re-enable HSE or HSI and configure PLL
}
|